package cn.frank.framework.caesar.module.ai.langChain4j.conf.mvc;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author frank
 * @ClassName: springMVC配置 ConfigForWebMvc
 * @Description: 注意: 不要继承extends WebMvcConfigurationSupport, boot项目只能生效一个,这样是有缺陷的
 * @email renjiangyang@163.com
 * @date 2021年4月27日 上午11:06:23
 * @Copyright: 2020 renjiangyang  All rights reserved.
 */
@Configuration
@Slf4j
@Data
public class WebMvcConfig implements WebMvcConfigurer {

    public WebMvcConfig() {
        super();
        log.info("\033[34m [caesar-module-ai-langChain4jm3u8] loading WebAutoMvcConfig \033[0m");
    }

    /**
     * 资源文件 配置
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        List<String> resources = new ArrayList();
        resources.add("classpath:/resources/");
        resources.add("classpath:/static/");
        resources.add("classpath:/public/");
        resources.add("classpath:/key/");
        for (WebMvcResourceEnum enumConstant : WebMvcResourceEnum.values()) {
            //enumConstant.clean(true);
            enumConstant.init();
            resources.add(enumConstant.getPath());
        }
        ResourceHandlerRegistration registy = registry.addResourceHandler("/upload/**");
        resources.forEach(resource -> {
            registy.addResourceLocations(resource);
            log.info("\033[34m      |-- resource={}\033[0m", resource);
        });
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 资源路径转发
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
        registry.addViewController("/frank").setViewName("redirect:doc.html");
    }

}
